package per.zqx.service.impl;

import org.hibernate.criterion.Example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.query.EscapeCharacter;
import org.springframework.stereotype.Service;
import per.zqx.mapper.OrderMapper;
import per.zqx.mapper.OrderMessageMapper;
import per.zqx.pojo.Order;
import per.zqx.pojo.OrderMessage;
import per.zqx.service.OrderMessageService;
import per.zqx.service.OrderService;

import java.time.LocalDateTime;
import java.util.Optional;

/**
 * DispatcherServiceImpl,
 *
 * @author Sa, Zhang 2021-08-15 16:41
 */
@Service
public class OrderMessageServiceImpl implements OrderMessageService {
    @Autowired
    OrderMessageMapper orderMessageMapper;

    @Override
    public OrderMessage add(Order order) {
        OrderMessage orderMessage = new OrderMessage();
        orderMessage.setOrderId(order.getId());
        orderMessage.setStatus(0);
        orderMessage.setCreateTime(LocalDateTime.now());
        orderMessage.setUpdateTime(LocalDateTime.now());
         return orderMessageMapper.save(orderMessage);
    }

    @Override
    public void update(Long orderId) {

        OrderMessage byOrderId = orderMessageMapper.findByOrderId(orderId);
        byOrderId.setStatus(1);
        byOrderId.setUpdateTime(LocalDateTime.now());
        orderMessageMapper.save(byOrderId);
    }

    @Override
    public OrderMessage getFirstFailOrder() {
        return Optional.ofNullable(orderMessageMapper.findFirstByStatus(0)).orElse(null);
    }
}
